import fs from "fs";

// 这里提供写文件画框的函数
//
//
//
import { jsPDF, TextOptionsLight } from "jspdf";
import "./heiti-normal.js";

export interface Point {
  x: number;
  y: number;
}

export interface Rect {
  w: number;
  h: number;
}

export type Gap = Point;

export interface FontOption {
  name: string;
  size: number;
  color: string;
}

type TextAlign = "left" | "center" | "right" | "justify" | undefined;

export function addFontToPdf(pdf: jsPDF, fontName: string, fontPath: string) {
  const myFont = fs.readFileSync(fontPath); // load the *.ttf font file as binary string
  pdf.addFileToVFS(`${fontName}.ttf`, myFont.toString("binary"));
  pdf.addFont(`${fontName}.ttf`, fontName, "normal");
  pdf.setFont(fontName);

  return pdf;
}

/**
 *
 * @param pdf
 * @param pos 写字的位置
 * @param text
 * @param align
 * @param maxWidth 最宽文字
 * @param fontOption
 */
export function drawText(
  pdf: jsPDF,
  pos: Point,
  text: string,
  align: TextAlign,
  maxWidth: number,
  fontOption: FontOption,
) {
  const { name, size, color } = fontOption;
  const { x, y } = pos;

  pdf.setFont(name, "regular", "normal");
  pdf.setFontSize(size);
  pdf.setTextColor(color);

  const textOpt = {
    align,
    maxWidth,
  };

  pdf.text(text, x, y, textOpt);
}
